﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace velha
{
    class Program
    {
        
        static void Main(string[] args)
        {
            int player = 1;
            Console.WriteLine("Jogo da Velha\n");
            string[,] table = new string[3, 3] 
            {
             {"_ ","_ ","_ "},
             {"_ ","_ ","_ "},
             {"_ ","_ ","_ "}
            };

            do
            {
                Console.Write(table[0, 0]);
                Console.Write(table[0, 1]);
                Console.Write(table[0, 2] + "  1 2 3\n");
                Console.Write(table[1, 0]);
                Console.Write(table[1, 1]);
                Console.Write(table[1, 2] + "  4 5 6\n");
                Console.Write(table[2, 0]);
                Console.Write(table[2, 1]);
                Console.Write(table[2, 2] + "  7 8 9\n\n");

                int escolha;

                if (player == 2)
                {
                    Random rnd = new Random();
                     escolha = rnd.Next(0, 10);
                     Convert.ToInt32(escolha);
                }

                else
                {
                    Console.WriteLine("Player" + player + " escolha um numero");
                    escolha = Convert.ToInt32(Console.ReadLine());
                }

                switch (escolha)
                {
                    case 1:
                        if ((table[0, 0] == "X ") || (table[0, 0] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                        else if (player == 1)
                        {
                            table[0, 0] = "X ";
                            player++;
                        }
                        else if  (player == 2)
                        {
                            table[0, 0] = "O ";
                            player--;
                        }
                        break;
                    case 2:
                        if ((table[0, 1] == "X ") || (table[0, 1] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                       else if (player == 1)
                        {
                            table[0, 1] = "X ";
                            player++;
                        }
                       else if (player == 2)
                        {
                            table[0, 1] = "O ";
                            player--;
                        }
                        break;
                    case 3:
                        if ((table[0, 2] == "X ") || (table[0, 2] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                        else if (player == 1)
                        {
                            table[0, 2] = "X ";
                            player++;
                        }
                        else if (player == 2)
                        {
                            table[0, 2] = "O ";
                            player--;
                        }
                        break;
                    case 4:
                        if ((table[1, 0] == "X ") || (table[1, 0] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                        else if (player == 1)
                        {
                            table[1, 0] = "X ";
                            player++;
                        }
                        else if (player == 2)
                        {
                            table[1, 0] = "O ";
                            player--;
                        }
                        break;
                    case 5:
                        if ((table[1, 1] == "X ") || (table[1, 1] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                        else if (player == 1)
                        {
                            table[1, 1] = "X ";
                            player++;
                        }
                        else if (player == 2)
                        {
                            table[1, 1] = "O ";
                            player--;
                        }
                        break;
                    case 6:
                        if ((table[1, 2] == "X ") || (table[1, 2] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                        else if (player == 1)
                        {
                            table[1, 2] = "X ";
                            player++;
                        }
                        else if (player == 2)
                        {
                            table[1, 2] = "O ";
                            player--;
                        }
                        break;
                    case 7:
                        if ((table[2, 0] == "X ") || (table[2, 0] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                        else if (player == 1)
                        {
                            table[2, 0] = "X ";
                            player++;
                        }
                        else if (player == 2)
                        {
                            table[2, 0] = "O ";
                            player--;
                        }
                        break;
                    case 8:
                        if ((table[2, 1] == "X ") || (table[2, 1] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                        else if (player == 1)
                        {
                            table[2, 1] = "X ";
                            player++;
                        }
                        else if (player == 2)
                        {
                            table[2, 1] = "O ";
                            player--;
                        }
                        break;

                    case 9:
                        if ((table[2, 2] == "X ") || (table[2, 2] == "O "))
                        {
                            if (player == 1)
                            {
                                Console.WriteLine("Esse numero já foi jogado, tecle <Enter> para escolha outro!");
                                Console.ReadKey();
                            }
                        }
                        else if (player == 1)
                        {
                            table[2, 2] = "X ";
                            player++;                            
                        }
                        else if (player == 2)
                        {
                            table[2, 2] = "O ";
                            player--;                            
                        }

                        break;
                }
                Console.Clear();
                
               if (((table[0, 0] == "X ") && (table[0, 1] == "X ") && (table[0, 2] == "X ")) ||
                   ((table[1, 0] == "X ") && (table[1, 1] == "X ") && (table[1, 2] == "X ")) ||
                   ((table[2, 0] == "X ") && (table[2, 1] == "X ") && (table[2, 2] == "X ")) ||
                   ((table[0, 0] == "X ") && (table[1, 0] == "X ") && (table[2, 0] == "X ")) ||
                   ((table[0, 1] == "X ") && (table[1, 1] == "X ") && (table[2, 1] == "X ")) ||
                   ((table[0, 2] == "X ") && (table[1, 2] == "X ") && (table[2, 2] == "X ")) ||
                   ((table[0, 0] == "X ") && (table[1, 1] == "X ") && (table[2, 2] == "X ")) ||
                   ((table[0, 2] == "X ") && (table[1, 1] == "X ") && (table[2, 0] == "X "))) 
                {
                    Console.Write("\n" + table[0, 0]);
                    Console.Write(table[0, 1]);
                    Console.Write(table[0, 2] + "\n");
                    Console.Write(table[1, 0]);
                    Console.Write(table[1, 1]);
                    Console.Write(table[1, 2] + "\n");
                    Console.Write(table[2, 0]);
                    Console.Write(table[2, 1]);
                    Console.Write(table[2, 2] + "\n");
                   Console.WriteLine("PLAYER 1 VENCEU"); Console.ReadKey(); Environment.Exit(0); 
                }

              if (((table[0, 0] == "O ") && (table[0, 1] == "O ") && (table[0, 2] == "O ")) ||
                  ((table[1, 0] == "O ") && (table[1, 1] == "O ") && (table[1, 2] == "O ")) ||
                  ((table[2, 0] == "O ") && (table[2, 1] == "O ") && (table[2, 2] == "O ")) ||
                  ((table[0, 0] == "O ") && (table[1, 0] == "O ") && (table[2, 0] == "O ")) ||
                  ((table[0, 1] == "O ") && (table[1, 1] == "O ") && (table[2, 1] == "O ")) ||
                  ((table[0, 2] == "O ") && (table[1, 2] == "O ") && (table[2, 2] == "O ")) ||
                  ((table[0, 0] == "O ") && (table[1, 1] == "O ") && (table[2, 2] == "O ")) ||
                  ((table[0, 2] == "O ") && (table[1, 1] == "O ") && (table[2, 0] == "O ")))
               {
                   Console.Write("\n" + table[0, 0]);
                   Console.Write(table[0, 1]);
                   Console.Write(table[0, 2] + "\n");
                   Console.Write(table[1, 0]);
                   Console.Write(table[1, 1]);
                   Console.Write(table[1, 2] + "\n");
                   Console.Write(table[2, 0]);
                   Console.Write(table[2, 1]);
                   Console.Write(table[2, 2] + "\n");
                   Console.WriteLine("PLAYER 2 VENCEU"); Console.ReadKey(); Environment.Exit(0);
               }
                

                

            } while ((table[0, 0] == "_ ") || (table[0, 1] == "_ ") || (table[0, 2] == "_ ") || (table[1, 0] == "_ ") || (table[1, 1] == "_ ") || (table[1, 2] == "_ ") || (table[2, 0] == "_ ") || (table[2, 1] == "_ ") || (table[2, 2] == "_ "));

            Console.Write("\n" + table[0, 0]);
            Console.Write(table[0, 1]);
            Console.Write(table[0, 2] + "\n");
            Console.Write(table[1, 0]);
            Console.Write(table[1, 1]);
            Console.Write(table[1, 2] + "\n");
            Console.Write(table[2, 0]);
            Console.Write(table[2, 1]);
            Console.Write(table[2, 2] + "\n");
            
            Console.WriteLine("Deu Velha");
            Console.ReadKey();
        }
    }
}
